﻿

const QFunc = {};
const Main_Tab_Name ="#maintabs"




QFunc.addTab = function (title, url, closable = true, icon = "") {

    if ($(Main_Tab_Name).tabs('exists', title)) {
        $(Main_Tab_Name).tabs('close', title);
    }
    $(Main_Tab_Name).tabs('add', {
        title: title || '新建',
        iconCls: icon,
        content: `<iframe src="${url}" frameborder="0"  scrolling="no" style="height:99%;width:100%;"></iframe>`,
        closable: closable,
        tools: [{ iconCls: "icon-mini-refresh", handler: QFunc.refreshTab }]
    });
}

QFunc.jumpHome= function () {
   
    let tabs = $(Main_Tab_Name).tabs('tabs');
    let panel = $.array.first(tabs, function (val) {
        let opts = val.panel("options");
        return opts.title == MainPageEles.homePageTitle;
    })
    if (panel && panel.length) {
        let index = $(Main_Tab_Name).tabs('getTabIndex', panel);
        $(Main_Tab_Name).tabs("select", index);
    }

}
QFunc.closeCurrentTab = function () {
    let tab = $(Main_Tab_Name).tabs("getSelected");
    if (tab) {
        let options = tab.panel("options");
        if (options && options.closable == false) {
            G.alert("当前tab不可以关闭");
            return;
        }
        let index = $(Main_Tab_Name).tabs("getTabIndex", tab);
        $(Main_Tab_Name).tabs("close", index);
    }
}

QFunc.closeTabExceptCurrent= function () {
  
    let tab = $(Main_Tab_Name).tabs("getSelected");
    let targettitle = tab.panel("options").title;
    let tabs = $(Main_Tab_Name).tabs('tabs');
    for (let i = 0; i < tabs.length; i++) {
        let options = tabs[i].panel("options");
        let title = tabs[i].panel("options").title;
        if (options && options.closable == false) {
            continue;
        }
        if (title && title != targettitle) {
            $(Main_Tab_Name).tabs("close", i);
        }
    }
},
QFunc.closeTabLeft= function () {
    
    let tabs = $(Main_Tab_Name).tabs('tabs');
    let tab = $(Main_Tab_Name).tabs("getSelected");
    let index = $(Main_Tab_Name).tabs("getTabIndex", tab);
    for (let i = 0; i < index; i++) {
        let options = tabs[i].panel("options");
        if (options && options.closable == false) {
            continue;
        }
        $(Main_Tab_Name).tabs("close", i);
    }


},
QFunc.closeTabRight= function () {
  
    let tabs = $(Main_Tab_Name).tabs('tabs');
    let tab = $(Main_Tab_Name).tabs("getSelected");
    let index = $(Main_Tab_Name).tabs("getTabIndex", tab);
    for (let i = index + 1; i < tabs.length; i++) {
        let options = tabs[i].panel("options");
        if (options && options.closable == false) {
            continue;
        }
        $(Main_Tab_Name).tabs("close", i);
    }


},
QFunc.closeTabAll= function () {
    
    let tabs = $(Main_Tab_Name).tabs('tabs');
    let tab = $(Main_Tab_Name).tabs("getSelected");
    for (let i = 0; i < tabs.length; i++) {
        let options = tabs[i].panel("options");
        if (options && options.closable == false) {
            continue;
        }
        $(Main_Tab_Name).tabs("close", i);
    }

    }
QFunc.updateHash = function (index) {

    let tab = $(Main_Tab_Name).tabs("getTab", index);
    var opts = tab.panel("options");
    $(Main_Tab_Name).tabs("update", {
        tab: tab,
        options: opts,
    })

}

QFunc.refreshTab = function () {
   
    let panel = $(Main_Tab_Name).tabs("getSelected");
    panel=$(Main_Tab_Name).tabs("getSelected");
    let index = $(Main_Tab_Name).tabs('getTabIndex', panel);
    let tab = $(Main_Tab_Name).tabs('getTab', index);
    let opts = tab.panel('options');
    $(Main_Tab_Name).tabs('update', {
        tab: tab,
        options: opts
    })
}










window.QFunc = QFunc;